home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '87 / Source ƒ.sea / Source ƒ / emacs source ƒ / FILEIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-28  |  3.5 KB  |  149 lines  |  [TEXT/MARC]

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here. A better message writing scheme should
  4.  * be used.
  5.  */
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. FILE    *ffp;                           /* File pointer, all functions. */
  11.  
  12. /*
  13.  * Open a file for reading.
  14.  */
  15. ffropen(fn)
  16. char    *fn;
  17. {
  18.         if ((ffp=fopen(fn, "r")) == NULL)
  19.                 return (FIOFNF);
  20.         return (FIOSUC);
  21. }
  22.  
  23. /*
  24.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  25.  * (cannot create).
  26.  */
  27. ffwopen(fn)
  28. char    *fn;
  29. {
  30. #if     VMS
  31.         register int    fd;
  32.  
  33.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  34.         || (ffp=fdopen(fd, "w")) == NULL) {
  35. #else
  36.         if ((ffp=fopen(fn, "w")) == NULL) {
  37. #endif
  38.                 mlwrite("Cannot open file for writing");
  39.                 return (FIOERR);
  40.         }
  41.         return (FIOSUC);
  42. }
  43.  
  44. /*
  45.  * Close a file. Should look at the status in all systems.
  46.  */
  47. ffclose()
  48. {
  49. #if    MSDOS
  50.     fputc(26, ffp);        /* add a ^Z at the end of the file */
  51. #endif
  52.     
  53. #if     V7 | USG | BSD | (MSDOS & (LATTICE | MSC))
  54.         if (fclose(ffp) != FALSE) {
  55.                 mlwrite("Error closing file");
  56.                 return(FIOERR);
  57.         }
  58.         return(FIOSUC);
  59. #else
  60.         fclose(ffp);
  61.         return (FIOSUC);
  62. #endif
  63. }
  64.  
  65. /*
  66.  * Write a line to the already opened file. The "buf" points to the buffer,
  67.  * and the "nbuf" is its length, less the free newline. Return the status.
  68.  * Check only at the newline.
  69.  */
  70. ffputline(buf, nbuf)
  71. char    buf[];
  72. {
  73.         register int    i;
  74. #if    CRYPT
  75.     char c;        /* character to translate */
  76.  
  77.     if (cryptflag) {
  78.             for (i = 0; i < nbuf; ++i) {
  79.             c = buf[i] & 0xff;
  80.             crypt(&c, 1);
  81.             fputc(c, ffp);
  82.         }
  83.     } else
  84.             for (i = 0; i < nbuf; ++i)
  85.                     fputc(buf[i]&0xFF, ffp);
  86. #else
  87.         for (i = 0; i < nbuf; ++i)
  88.                 fputc(buf[i]&0xFF, ffp);
  89. #endif
  90.  
  91.         fputc('\n', ffp);
  92.  
  93.         if (ferror(ffp)) {
  94.                 mlwrite("Write I/O error");
  95.                 return (FIOERR);
  96.         }
  97.  
  98.         return (FIOSUC);
  99. }
  100.  
  101. /*
  102.  * Read a line from a file, and store the bytes in the supplied buffer. The
  103.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  104.  * at the end of the file that don't have a newline present. Check for I/O
  105.  * errors too. Return status.
  106.  */
  107. ffgetline(buf, nbuf)
  108. register char   buf[];
  109. {
  110.         register int    c;
  111.         register int    i;
  112.  
  113.         i = 0;
  114.  
  115.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  116.                 if (i >= nbuf-2) {
  117.             buf[nbuf - 2] = c;    /* store last char read */
  118.             buf[nbuf - 1] = 0;    /* and terminate it */
  119.                         mlwrite("File has long line");
  120. #if    CRYPT
  121.             if (cryptflag)
  122.                 crypt(buf, strlen(buf));
  123. #endif
  124.                         return (FIOLNG);
  125.                 }
  126.                 buf[i++] = c;
  127.         }
  128.  
  129.         if (c == EOF) {
  130.                 if (ferror(ffp)) {
  131.                         mlwrite("File read error");
  132.                         return (FIOERR);
  133.                 }
  134.  
  135.                 if (i != 0) {
  136.                         mlwrite("File has funny line at EOF");
  137.                         return (FIOERR);
  138.                 }
  139.                 return (FIOEOF);
  140.         }
  141.  
  142.         buf[i] = 0;
  143. #if    CRYPT
  144.     if (cryptflag)
  145.         crypt(buf, strlen(buf));
  146. #endif
  147.         return (FIOSUC);
  148. }
  149.